﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using BUS;
using DTO;
using System.Text;

namespace Cucre
{
    public partial class WebForm18 : System.Web.UI.Page
    {
        int typeReport;// = 0 report by cucre.vn
                       // = 1 report by provider
        int monthReport;
        int yearReport;
        DataClasses1DataContext data = new DataClasses1DataContext();
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string path = Server.MapPath("theme.txt");
            StreamReader SR = new StreamReader(path);
            string nameTheme = SR.ReadLine();
            SR.Close();
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", nameTheme);
                Page.Theme = (string)Session["MyTheme"];
            }
            else
            {
                Page.Theme = (string)Session["MyTheme"];
            }
            
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
             if (ThangBaoCao_DropDownList.Items.Count == 0)
              {
            for (int i = 1; i < 13; ++i)
                ThangBaoCao_DropDownList.Items.Add(" " + i.ToString("00") + " ");
              }
             if (NamBaoCao_DropDownList.Items.Count == 0)
             {
                 for (int i = DateTime.Today.Year; i >= 2005; --i)
                     NamBaoCao_DropDownList.Items.Add(" " + i.ToString() + " ");
             }
            typeReport = TypeReport_DropDownList.SelectedIndex;
            monthReport = int.Parse(ThangBaoCao_DropDownList.SelectedValue);
            yearReport = int.Parse(NamBaoCao_DropDownList.SelectedValue);
            if (!IsPostBack)
            {
                baoCaoThongKe_DataBound(-1);
               
            }
        }

        public void baoCaoThongKe_DataBound(int idProvider)
        {
           
            DateTime date=new DateTime(yearReport,monthReport,1);
            if (typeReport == 0)
            {
                var query = from product in data.PRODUCTs
                            where (((date - product.TIME_START.Value).TotalHours >= 0) || (product.TIME_START.Value.Month == monthReport)) && ((product.TIME_STOP.Value - date).TotalHours >= 0)
                            select new
                            {
                                IDPRODUCT = product.IDPRODUCT,
                                PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                Name_Product = product.Name_Product,
                                TIME_START = product.TIME_START.Value.ToShortDateString(),
                                TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                CITY = product.CITY,
                                TOTAL_ORDER = data.ORDERs.Count(order => ((order.IDPRODUCT == product.IDPRODUCT) && (order.TIME_ORDER.Value.Month == monthReport))),
                                TOTAL_INCOME = (from order in data.ORDERs where ((order.IDPRODUCT == product.IDPRODUCT) && (order.TIME_ORDER.Value.Month == monthReport)) select order.PAYMENT).Sum()
                            };
                BaoCaoTheoTinKhuyenMai_GridView.DataSource = query;
                BaoCaoTheoTinKhuyenMai_GridView.DataBind();
                Label_Month.Text = monthReport.ToString();
            }
            else
            {
                var query = from product in data.PRODUCTs
                            where (((date - product.TIME_START.Value).TotalHours >= 0) || (product.TIME_START.Value.Month == monthReport)) && ((product.TIME_STOP.Value - date).TotalHours >= 0) && (product.IDPROVIDER == idProvider)
                            select new
                            {
                                IDPRODUCT = product.IDPRODUCT,
                                PROVIDER_NAME = product.PROVIDER.COMPANY_NAME,
                                Name_Product = product.Name_Product,
                                TIME_START = product.TIME_START.Value.ToShortDateString(),
                                TIME_STOP = product.TIME_STOP.Value.ToShortDateString(),
                                CITY = product.CITY,
                                TOTAL_ORDER = data.ORDERs.Count(order => ((order.IDPRODUCT == product.IDPRODUCT) && (order.TIME_ORDER.Value.Month == monthReport))),
                                TOTAL_INCOME = (from order in data.ORDERs where ((order.IDPRODUCT == product.IDPRODUCT) && (order.TIME_ORDER.Value.Month == monthReport)) select order.PAYMENT).Sum()
                            };
                BaoCaoTheoTinKhuyenMai_GridView.DataSource = query;
                BaoCaoTheoTinKhuyenMai_GridView.DataBind();
            }
           
           float totalIncome = 0;
         
            for (int i = 0; i < BaoCaoTheoTinKhuyenMai_GridView.Rows.Count; ++i)
            {
                if (BaoCaoTheoTinKhuyenMai_GridView.Rows[i].Cells[6].Text.Trim() == "0")
                    BaoCaoTheoTinKhuyenMai_GridView.Rows[i].Cells[7].Text = "0";

                totalIncome += float.Parse(BaoCaoTheoTinKhuyenMai_GridView.Rows[i].Cells[7].Text.Trim());
            }
            LabelTotalIncome.Text = totalIncome.ToString();
           
        }

        public void DanhSachDoanhNghiep_DataBound()
        {
            if (typeReport == 1)
            {
                var query = from provider in data.PROVIDERs
                            select new
                              {
                                  IDPROVIDER = provider.IDPROVIDER,
                                  COMPANY_NAME = provider.COMPANY_NAME,
                                  EMAIL = provider.ACCOUNT.EMAIL,
                                  PHONE = provider.ACCOUNT.PHONE_NUMBER,
                                  ADDRESS = provider.ACCOUNT.ADDRESS
                              };
                DSDoanhNghiep_GridView.DataSource = query;
                DSDoanhNghiep_GridView.DataBind();
            }
        }

        protected void TypeReport_DropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (typeReport != TypeReport_DropDownList.SelectedIndex)
            //{
                typeReport = TypeReport_DropDownList.SelectedIndex;
                baoCaoThongKe_DataBound(-1);
                if (typeReport == 1)
                {
                    DivDanhSachDoanhNghiep.Visible = true;
                    DanhSachDoanhNghiep_DataBound();
                }
                else
                    DivDanhSachDoanhNghiep.Visible = false;
            //}
        }

        protected void ThangBaoCao_DropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
           // if (monthReport != int.Parse(ThangBaoCao_DropDownList.SelectedValue.Trim()))
            //{
                monthReport = int.Parse(ThangBaoCao_DropDownList.SelectedValue.Trim());
                baoCaoThongKe_DataBound(-1);
            //}
        }

        protected void NamBaoCao_DropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
           // if (yearReport != int.Parse(NamBaoCao_DropDownList.SelectedValue.Trim()))
            //{
                yearReport = int.Parse(NamBaoCao_DropDownList.SelectedValue.Trim());
                baoCaoThongKe_DataBound(-1);
           // }
        }

        protected void DSDoanhNghiep_GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            DSDoanhNghiep_GridView.SelectedIndex=e.NewSelectedIndex;
            int idProvider = int.Parse(DSDoanhNghiep_GridView.Rows[e.NewSelectedIndex].Cells[0].Text.Trim());
            baoCaoThongKe_DataBound(idProvider);
        //    PROVIDER PRO=ProviderBUS.GetProvider(idProvider);
            PROVIDER PRO = data.PROVIDERs.Single(pro => pro.IDPROVIDER == idProvider);
            Label_Month.Text = monthReport.ToString() + " : Công Ty  " + PRO.COMPANY_NAME;
        }

        protected void DSDoanhNghiep_GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            DSDoanhNghiep_GridView.PageIndex = e.NewPageIndex;
        }

       
    }
}
